/*
 * @lc app=leetcode.cn id=394 lang=javascript
 *
 * [394] 字符串解码
 */

// @lc code=start
/**
 * @param {string} s
 * @return {string}
 */
var decodeString = function(s) {
  if(!s) return s;
  let numStack = [];
  let cahrtStack = [];

  let repeat = '';
  let result = '';

  for(let c of s){
    if(!isNaN(c)){
      repeat += c;
    } else if(c === '['){
      numStack.push(repeat);
      cahrtStack.push(result);
      repeat = '';
      result = '';
    } else if(c === ']'){
      let curChart = cahrtStack.pop();
      let num = numStack.pop();

      result = curChart + result.repeat(num);
    } else {
      result += c;
    }
  }
  return result;
};
// @lc code=end

